Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor main.cpp setup #49362

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

stijn-h
Copy link
Contributor

@stijn-h stijn-h commented Jun 6, 2021

This PR refactors the setup code in main.cpp. When looking to contribute to a project, I always start with the program entrypoint. I think main.cpp being a bit of a mess sets off a lot of new contributors. It's easy to get lost in chunks of code with overlapping logic and the dependencies between various singletons isn't conveyed very well. I hope this makes it easier for new users to understand the engine.

Because things were so tangled up, it wasn't easy to isolate a specific part and just refactor that. So in the end, most of the setup code has been refactored, but only a few intentional changes have been introduced. That increases the chance of a regression, so it should obviously be tested in as many ways as possible.

Main goals

The PR mostly restructures and groups code in a way that makes it easier to understand program flow and dependencies between certain classes. I think the code should speak for itself, but a short explanation might help with reviewing, and if we at least agree on these principles, it makes it easier to discuss the implementation.

Separating distinct phases.

While the interface of main was easy to understand, under the hood all kinds of things were blended together. I have tried to separate the code in Main into the following stages:

  1. Parsing command line
  2. Loading project settings
  3. Finalizing the configuration
  4. Initialization
  5. Starting (running tool or creating main loop)
  6. Iteration (no changes)

Clear setup based on application type.

To me, I felt that it's important to understand what kind of application we want to run (project, script, editor, project manager, a tool) to understandi the whole initialization process. Currently, it's not clear until the very last moment. It also seems like most code assumes that we are running the editor or project manager, while running a project is somewhat of an afterthought. I have made the application type explicit and it is specified as early as possible.

Important here is the ApplicationConfiguration, which holds most of the variables for initializing singletons. It first gets initialized in step 1. Then, after loading project settings, it is manually adjusted in step 3, after which it doesn't have to change anymore. That leaves us with an invariant configuration for the rest of the program, which makes the code much simpler to understand. Not much has fundamentally changed to step 4 (initialization), because I don't have enough knowledge of this part. Step 6 has not changed at all.

Intentional changes

In the CLI, there were a few arguments that only had meaning in combination with other arguments. They have been changed so that they are optional parameters to those arguments.

  • --export-debug, --export-pack are no longer separate commands. Instead you use --export <preset> <path> [option], where option can be --export-debug or --export-pack. Options can be combined in any order.
  • --no-docbase is no longer a separate command. Instead you use --doc-tool <path> --no-docbase.
  • --check-only is no longer a separate command. Instead you use --script <path> --check-only.

EditorSettings is now created in main.cpp instead of duplicated in Editor and PM.

A few open questions

  • Default project settings are now grouped in a method, but maybe they should be moved to corresponding singletons/servers.
  • Personally, I don't like the conditional automatic chaining between setup and finish_setup(setup2). I think it would be better to force every platform to call both methods, but I don't know if that is a good solution.
  • Display driver project setting defaults to Vulkan, but isn't that supposed to be the rendering driver? It currently always fails and defaults to something else.

Things that are not in this PR, but could be looked into in the future:

  • Tools each require a different amount of initialisation. At the moment the engine is fully initialised regardless of which tool you run. This also means a window is created by default. We could set no-window flag, but that seems to be broken on master at the moment. Maybe it should default to headless?
  • Exporting through CLI still requires the editor node. Just like other tools, it should run without creating a main loop.
  • There are some leftover methods (e.g. is_project_manager) that could be refactored/relocated.
  • Currently, physics, navigation etc. is initialized and running when running the Project Manager.

Initialization process broken up into multiple parts.
- Parsing command line
- Loading project settings
- Finalizing configuration
- Initialization
- Starting (running tool or creating main loop)
@akien-mga
Copy link
Member

Just mentioning that this is on my radar, I'll need time to do a thorough review and testing, but in general I like the direction it's going, and it's extremely needed.

@stijn-h
Copy link
Contributor Author

stijn-h commented Jun 19, 2021

Just mentioning that this is on my radar, I'll need time to do a thorough review and testing, but in general I like the direction it's going, and it's extremely needed.

That's good to hear. My intent was to give this a shot and see how far I can go without actually changing all that much. That means there is room for some changes in command line or initialization logic. But I think it's better if this PR is mostly just a refactor, that should make it easier to implement actual changes later on.

@Shatur
Copy link
Contributor

Shatur commented Aug 14, 2021

The PR looks great!

I want only leave a note that #44594 could be using for parsing in ApplicationConfiguration. This will allow us to generate help automatically (less bug-prone), support for standard syntax (like compound arguments (-abc), sticky arguments (-dvalue), adjacent arguments (-d=value)), specify logic more explicitly (by decoupling text parsing and argument validation) and reuse this functionality in dedicated servers or even games by users.
This feature is currently available in Goost, unless the developers change their mind about using manual parsing arguments.

@stijn-h
Copy link
Contributor Author

stijn-h commented Aug 14, 2021

I want only leave a note that #44594 could be using for parsing in ApplicationConfiguration.

It probably could and I would be interested to see it used on Godot args. But sadly, it seems like we are kind of stuck in limbo at the moment. On the one hand, the devs have not been too outspoken about a separate parser, so it doesn't make sense for me to use it in this PR until they change their minds. On the other hand, parsing Godot args would be a good use case for your parser and it would not be possible without some major refactoring to main.cpp like in this PR. So, I am not sure what is the best thing to do.

@stijn-h
Copy link
Contributor Author

stijn-h commented Aug 19, 2021

@akien-mga I have tried rebasing but it's kind of a nightmare as you can imagine. What do you think should happen with this PR? Frankly, I find it strange that there hasn't been any feedback from anyone in two months, given what I outlined in the PR and what has been said about the state of main.cpp. In the mean time, lots of hacks continue to be added, making it only harder to work on this PR. I would like to continue to work on it, but that only makes sense if I have confidence this PR will be reviewed and is likely to be merged at some point.

@akien-mga
Copy link
Member

Sorry for the lack of update, with 20 PRs per day and 1200 PRs of backlog my attention is spread thin. As I mentioned previously, I think this goes in the right direction and I want to take time to review in depth and merge at least parts if not all of it. But we discussed with @reduz a few weeks ago that it's something that should wait for once 4.0 has been released, as we're too busy with other tasks for now. So we'll revisit that once we can give it more attention and make sure that everything works as intended, without the high entropy of a daily refactored master branch.

Until then, it might be tedious to rebase this regularly so I would suggest leaving it in limbo for some months until we're ready to work on it together.

@stijn-h
Copy link
Contributor Author

stijn-h commented Aug 20, 2021

I understand you are busy and I am fine with that decision, I just needed some clarity.

@aaronfranke
Copy link
Member

@stijn-h Will you be able to work on this PR? It needs to be rebased before it can be considered. Considering it has not been updated for over 2 years, it may need to be remade from scratch.

@aaronfranke aaronfranke marked this pull request as draft October 7, 2023 19:40
@aaronfranke aaronfranke modified the milestones: 4.2, 4.x Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants